﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace Cn.Edwon.Web.Script {
    public class JsUtil {

        public static String JsonDataFilter(String rVal) {
            if (!String.IsNullOrEmpty(rVal)) {
                rVal = rVal.Replace("\r", "");
                rVal = rVal.Replace("\n", "");
                rVal = rVal.Replace("\\", "\\\\");
                rVal = rVal.Replace("\"", "\\\"");
            }
            return rVal;
        }
        public static String JsonDataFilter(Object rVal) {
            if (rVal != null) {
                return JsonDataFilter(rVal.ToString());
            } else {
                return "";
            }
        }
        #region EO转Script数据
        #region EO转Json
        /// <summary>
        /// 将一个对象转义为json格式的数据
        /// </summary>
        /// <param name="eo"></param>
        /// <returns></returns>
        public static String ParseEO2Json(Object eo) {
            return ParseEO2Json(eo, null);
        }
        /// <summary>
        /// 将一个对象转义为json格式的数据
        /// </summary>
        /// <param name="eo"></param>
        /// <param name="rProperties">指定获取哪些字段</param>
        /// <returns></returns>
        public static String ParseEO2Json(Object rEO,String rProperties) {
            if (rEO == null) return "{}";
            try {
                StringBuilder sb = new StringBuilder();
                sb.Append("{");
                PropertyInfo[] pl = rEO.GetType().GetProperties();
                int count = 0;
                if (rProperties == null) {
                        for(int i=0;i<pl.Length;i++){
                            PropertyInfo pi = pl[i];
                            if (i > 0) {
                                sb.Append(",");
                            }
                            object oVal = pi.GetValue(rEO, null);
                            String val = (oVal == null) ? "" : oVal.ToString();
                            String strVal = HtmlUtil.EncodeHtml(val);
                            strVal = strVal.Replace("\"", "\\\"");
                            strVal = strVal.Replace("\r", "");
                            strVal = strVal.Replace("\n", "");
                            sb.Append("\"" + pi.Name + "\":\"" + strVal + "\"");
                        }
                } else {
                    String[] _arrs = rProperties.Split(',');
                    sb.Append("\"");
                    Object key = rEO.GetType().GetProperty(_arrs[0]).GetValue(rEO, null);
                    String keyVal = (key == null) ? "" : key.ToString();
                    keyVal = HtmlUtil.EncodeHtml(keyVal);
                    keyVal = keyVal.Replace("\"", "\\\"");
                    keyVal = keyVal.Replace("\r", "");
                    keyVal = keyVal.Replace("\n", "");
                    sb.Append("\"" + _arrs[0] + "\":\"" + keyVal + "\"");
                    sb.Append("\",\"");
                    if (_arrs.Length > 0) {
                        for (int i = 1; i < _arrs.Length; i++) {
                            String _arr = _arrs[i];
                            if (count > 0) {
                                sb.Append(",");
                            }
                            object oVal = rEO.GetType().GetProperty(_arr).GetValue(rEO, null);
                            String val = (oVal == null) ? "" : oVal.ToString();
                            String strVal = HtmlUtil.EncodeHtml(val);
                            strVal = strVal.Replace("\"", "\\\"");
                            strVal = strVal.Replace("\r", "");
                            strVal = strVal.Replace("\n", "");
                            sb.Append("\"" + _arr + "\":\"" + strVal + "\"");
                            count++;
                        }
                    }
                    sb.Append("\"");
                }
                sb.Append("}");
                return sb.ToString();
            } catch (Exception ex) {
                return "{\"ex\",\""+ex.Message+"\"}";
            }
        }
        /// <summary>
        /// 将一个对象列表转义为json格式的数据
        /// </summary>
        /// <param name="eo"></param>
        /// <returns></returns>
        public static String ParseEOList2Json(IList<Object> list) {
            return ParseEOList2Json(list, null);
        }
        public static String ParseEOList2Json<T>(IList<T> list) {
            return ParseEOList2Json(list,null);
        }
        public static String ParseEOList2Json<T>(IList<T> list, String rProperties) {
            return ParseEOListObj2Json(list, rProperties);

        }
        public static String ParseEOList2Json(IList list, String rProperties) {
            return ParseEOListObj2Json(list, rProperties);
        }
        public static String ParseEOList2Json(IList<Object> list, String rProperties) {
            return ParseEOListObj2Json(list, rProperties);
        }

        /// <summary>
        /// 将一个对象列表转义为json格式的数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rProperties">指定获取哪些字段</param>
        /// <returns></returns>
        private static String ParseEOListObj2Json(Object list, String rProperties) {
            IList _list = (IList)list;
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i = 0; i < _list.Count; i++) {
                if (i > 0)
                    sb.Append(",");
                sb.Append(ParseEO2Json(_list[i], rProperties));
            }
            sb.Append("]");
            return sb.ToString();
        }
        #endregion 

        #region EO转Array
        /// <summary>
        /// 将一个对象列表转义为json格式的数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="rProperties">指定获取哪些字段</param>
        /// <returns></returns>
        public static String ParseEOList2Array(IList<Object> list, String rProperties) {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i = 0; i < list.Count; i++) {
                if (i > 0)
                    sb.Append(",");
                sb.Append(ParseEO2Array(list[i], rProperties));
            }
            sb.Append("]");
            return sb.ToString();
        }
        public static String ParseEOList2Array<T>(IList<T> list, String rProperties) {
            IList<Object> l = new List<Object>();
            foreach (T eo in list) {
                l.Add(eo);
            }
            return ParseEOList2Array(l, rProperties);
        }
        public static String ParseEOList2Array<T>(IList list, String rProperties) {
            IList<Object> l = new List<Object>();
            foreach (object eo in list) {
                l.Add(eo);
            }
            return ParseEOList2Array(l, rProperties);
        }
        /// <summary>
        /// 将一个对象转义为数组格式的数据
        /// </summary>
        /// <param name="eo"></param>
        /// <param name="rProperties">指定获取哪些字段</param>
        /// <returns></returns>
        public static String ParseEO2Array(Object rEO, String rProperties) {
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            try {
                PropertyInfo[] pl = rEO.GetType().GetProperties();
                if (String.IsNullOrEmpty(rProperties)) {
                    for (int i = 0; i < pl.Length; i++) {
                        PropertyInfo pi = pl[i];
                        if (i > 0) {
                            sb.Append(",");
                        }
                        sb.Append("\"");
                        object oVal = pi.GetValue(rEO, null);
                        String val = (oVal == null) ? "" : oVal.ToString();
                        String strVal = HtmlUtil.EncodeHtml(val);
                        strVal = strVal.Replace("\"", "\\\"");
                        strVal = strVal.Replace("\r", "");
                        strVal = strVal.Replace("\n", "");
                        sb.Append(strVal);
                        sb.Append("\"");
                    }
                } else {
                    int count = 0;
                    String[] _arrs = rProperties.Split(',');
                    sb.Append("\"");
                    Object key = rEO.GetType().GetProperty(_arrs[0]).GetValue(rEO, null);
                    String keyVal = (key == null) ? "" : key.ToString();
                    keyVal = HtmlUtil.EncodeHtml(keyVal);
                    keyVal = keyVal.Replace("\"", "\\\"");
                    keyVal = keyVal.Replace("\r", "");
                    keyVal = keyVal.Replace("\n", "");
                    sb.Append(keyVal);
                    sb.Append("\",\"");
                    if (_arrs.Length > 0) {
                        for (int i = 1; i < _arrs.Length; i++) {
                            String _arr = _arrs[i];
                            if (count > 0) {
                                sb.Append(",");
                            }
                            object oVal = rEO.GetType().GetProperty(_arr).GetValue(rEO, null);
                            String val = (oVal == null) ? "" : oVal.ToString();
                            String strVal = HtmlUtil.EncodeHtml(val);
                            strVal = strVal.Replace("\"", "\\\"");
                            strVal = strVal.Replace("\r", "");
                            strVal = strVal.Replace("\n", "");
                            sb.Append(strVal);
                            count++;
                        }
                    }
                    sb.Append("\"");
                }
            } catch (Exception ex) {
                return "[\"\",\""+ex.Message+"\"]";
            }
            sb.Append("]");
            return sb.ToString();
        }
        #endregion

        #endregion
    }
}
